﻿using BLL.Base;
using BLL.custom;
using CommandService;
using Entity;
using Entity.DbEntity;
using Entity.ViewEntity;
using Microsoft.SqlServer.Server;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;


namespace BLL.Statistics
{
    public class StatisticsBLL : PubMethod
    {
        Commands cmd;
        LoginInfo userLogin;
        List<OperateModel> OperList;
        OperateModel oper;
        ReturnResult rsl;
        SysFrameWork.SystemBLL sysBLL;
        StringBuilder sb;
        CuDbHelper cdb;
        Tools.TxtLogHelper txt;

        public StatisticsBLL()
        {
            cmd = new Commands();
            userLogin = new LoginInfo();
            sysBLL = new SysFrameWork.SystemBLL();
            cdb = new CuDbHelper();
            txt = new Tools.TxtLogHelper();
        }

        /// <summary>
        /// 条码溯源追踪
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public ReturnResult BarCodeSoures(string datas)
        {
            rsl = new ReturnResult();
            try
            {
                var json = JsonConvert.DeserializeObject<JObject>(datas);
                if (string.IsNullOrWhiteSpace(json["barCode"].ToString()))
                {
                    rsl.Message = "请输入查询条码";
                    return rsl;
                }
                StringBuilder sb = new StringBuilder();
                sb.Append("select barCode,proCode,proName,spec,isnull(relationBar,'') relationBar from V_BarCode_Info ");
                sb.Append("where baseCode='" + userLogin.UserInfo().baseCode + "'");
                var table = cmd.GetDataTable(sb.ToString());//获取全部条码
                List<string> tlist = new List<string>();
                loopAssembleRecords(table, json["barCode"].ToString(), ref tlist);
                if (tlist.Count > 0)
                {
                    List<Dictionary<string, object>> menuList = new List<Dictionary<string, object>>();
                    foreach (string bar in tlist)
                    {
                        var dr = table.Select("barCode='" + bar + "'");
                        string tStr = "";
                        if (dr.Length > 0)
                        {
                            tStr = string.Format("({0}-{1})", dr[0]["proName"].ToString(), dr[0]["spec"].ToString());

                        }
                        Dictionary<string, object> fdic = new Dictionary<string, object>();
                        fdic["title"] = string.Format("{0}-{1}", bar, tStr);
                        fdic["id"] = bar;
                        List<Dictionary<string, object>> cList = new List<Dictionary<string, object>>();
                        this.ChildUrl(table, bar, ref cList);
                        fdic["children"] = cList;
                        menuList.Add(fdic);
                    }
                    if (menuList.Count > 0)
                    {
                        rsl.Data = menuList;
                        rsl.Result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 判断是否是最顶级节点
        /// </summary>
        /// <param name="table"></param>
        /// <param name="barCode"></param>
        /// <param name="tlist"></param>
        public void loopAssembleRecords(DataTable table, string barCode, ref List<string> tlist)
        {
            var dr = table.Select("barCode='" + barCode + "'");
            if (dr.Length < 1)
            {
                tlist.Add(barCode);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(dr[0]["relationBar"].ToString()))
                {
                    loopAssembleRecords(table, dr[0]["relationBar"].ToString(), ref tlist);
                }
                else
                {
                    tlist.Add(barCode);
                }

            }
        }
        /// <summary>
        /// 子节点
        /// </summary>
        /// <param name="table"></param>
        /// <param name="cCode"></param>
        /// <param name="chlist"></param>
        private void ChildUrl(DataTable table, string cCode, ref List<Dictionary<string, object>> chlist)
        {
            var dr = table.Select("relationBar='" + cCode + "'");//获取父节点下面的全部子节点
            if (dr.Length > 0)
            {
                for (int x = 0; x < dr.Length; x++)
                {
                    string tStr = "";
                    tStr = string.Format("({0}-{1})", dr[x]["proName"].ToString(), dr[x]["spec"].ToString());

                    List<Dictionary<string, object>> tempList = new List<Dictionary<string, object>>();
                    var cdic = new Dictionary<string, object>();
                    cdic["title"] = string.Format("{0}-{1}", dr[x]["barCode"].ToString(), tStr);
                    cdic["id"] = dr[x]["barCode"].ToString();
                    ChildUrl(table, dr[x]["barCode"].ToString(), ref tempList);
                    cdic["children"] = tempList;
                    chlist.Add(cdic);
                }
            }
        }

       
        /// <summary>
        /// 条码数据显示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        private List<Dictionary<string, string>> barCodesList(string pageIndex, string pageSize, ref string pageCount, string condition)
        {

            StringBuilder sb = new StringBuilder();
            var json = JsonConvert.DeserializeObject<JObject>(condition);
            sb.Append("baseCode='" + userLogin.UserInfo().baseCode + "'");
            if (!string.IsNullOrWhiteSpace(json["sDate"].ToString()))
            {//开始时间
                sb.Append(" and convert(date,happenDate, 20)" + " >='" + json["sDate"].ToString() + "' ");
            }
            if (!string.IsNullOrWhiteSpace(json["eDate"].ToString()))
            {//结束时间
                sb.Append(" and convert(date,happenDate, 20)" + "<= '" + json["eDate"].ToString() + "' ");
            }
            if (!string.IsNullOrWhiteSpace(json["proCode"].ToString()))
            {//结束时间
                sb.Append(" and proCode= '" + json["proCode"].ToString() + "' ");
            }
            return cmd.PageList("V_BarCode_Info", pageIndex, pageSize, sb.ToString(), "barCode desc", ref pageCount);
        }


    /// <summary>
    /// 获取季度所属月份
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
        private string getQuarter(int? month,bool isPass=false)
        {
            if (month == null)
            {
                month = DateTime.Now.Month;
            }
            else if(month<1)
            {
                return "10,11,12";
            }
            if (isPass)
            {
                month = month - 3;
                return this.getQuarter(month, false);
            }
            else
            {
                if (month < 4)
                {
                    return "1,2,3";
                }
                else if (month > 3 && month < 7)
                {
                    return "4,5,6";
                }
                else if (month > 6 && month < 10)
                {
                    return "7,8,9";
                }
                else
                {
                    return "10,11,12";
                }
            }
           
           

        }


    }
}
